﻿using DomainEventDemo.EntityFrameworkCore;
using EasyAbp.Abp.EventBus.Cap;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Autofac;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.SqlServer;
using Volo.Abp.Modularity;

namespace DomainEventDemo.App
{
    [DependsOn(
        typeof(AbpAutofacModule),
        typeof(AbpAspNetCoreMvcModule),
        typeof(AbpEntityFrameworkCoreSqlServerModule),
        typeof(OrderingDomainEventDemoApplicationModule),
        typeof(OrderingDomainEventDemoEntityFrameworkCoreModule),
        typeof(AbpEventBusCapModule)
        )]
    public class FixtServiceHostModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();

            Configure<AbpDbContextOptions>(options =>
            {
                options.UseSqlServer();
            });

            Configure<AbpDbConnectionOptions>(options =>
            {
                options.ConnectionStrings.Default = configuration.GetConnectionString("Default");
            });

            context.Services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo { Title = "Ordering Event Demo API", Version = "v1" });
                options.DocInclusionPredicate((docName, description) => true);
                options.CustomSchemaIds(type => type.FullName);
            });

            Configure<AbpAspNetCoreMvcOptions>(options =>
            {
                options.ConventionalControllers
                    .Create(typeof(OrderingDomainEventDemoApplicationModule).Assembly, optionsAction =>
                    {
                        optionsAction.RootPath = "OrderingDomainEventDemo";
                    });
            });

            context.AddCapEventBus(capOptions =>
            {
                capOptions.UseSqlServer(configuration.GetConnectionString("Default"));
                capOptions.UseRabbitMQ(options =>
                {
                    options.UserName = "admin1";
                    options.Password = "admin123";
                    options.HostName = "localhost";
                    options.Port = 5672;
                    options.VirtualHost = "/";
                });
                capOptions.UseDashboard();
            });

            Configure<AbpBackgroundJobOptions>(options =>
            {
                options.IsJobExecutionEnabled = false;
            });
        }

        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();

            app.UseRouting();
            app.UseConfiguredEndpoints();

            app.UseSwagger();
            app.UseSwaggerUI(options =>
            {
                options.SwaggerEndpoint("/swagger/v1/swagger.json", "Domain Event Demo API");
            });
        }
    }
}
